package com.noble.glamour.ui.agora.act.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.noble.glamour.R;
import com.noble.glamour.bean.my.GiftsList;
import com.noble.glamour.third.recyclerholder.BaseRecyclerViewHolder;
import com.noble.library.utils.ScreenUtils;
import com.noble.library.view.base.BasePanel;

import java.util.List;

/**
 * 礼物列表：赠送礼物弹框
 * Created by Su on 2017/7/22.
 */
public class RtcVideoGiftPanel extends BasePanel implements BaseRecyclerViewHolder.OnItemClickListener {
    private float toDpMutliple = 1; //根据屏幕密度动态设置宽高

    private RecyclerView recyclerView;
    private GiftPanelAdapter adapter;
    private List<GiftsList.GiftInfo> list;

    public RtcVideoGiftPanel(Context context, List<GiftsList.GiftInfo> list) {
        super(context);
        setContentView(R.layout.f1_gift_panel_dialog);
        this.list = list;
        initView();
    }

    private void initView() {
        toDpMutliple = ScreenUtils.toDpMultiple((Activity) getContext());
        recyclerView = (RecyclerView) findViewById(R.id.gift_list);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        recyclerView.addItemDecoration(new ItemSpaces((int) (9 * toDpMutliple)));

        adapter = new GiftPanelAdapter();
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        adapter.setList(list);
    }

    @Override
    public void onItemClick(View convertView, int position) {
        adapter.updateData(position);
        if (listener != null)
            listener.OnPanelItemClick(convertView, position);
    }

    // =============== 对外提供的Panel列表点击监听 ========================
    private GiftPanelItemClickListener listener;

    public void setPanelItemClickListener(GiftPanelItemClickListener listener) {
        this.listener = listener;
    }

    public interface GiftPanelItemClickListener {
        void OnPanelItemClick(View convertView, int position);
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {
        private int space;

        public ItemSpaces(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.right = space;
            outRect.left = space;
            outRect.top = space;
        }
    }
}
